<?php
/**
 * Authenticate check for admin area
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @category   Module
 * @uses       Zend_Db_Table_Abstract
 * @package    Login
 * @subpackage Model
 * @todo
 */

class Login_Model_Auth extends Zend_Db_Table_Abstract
{
    /**
     * User id
     * 
     * @var string 
     */
    protected $_userId;

    /**
     * Rolename
     * 
     * @var string 
     */
    protected $_rolename;


    /**
     * Role id
     * 
     * @var string 
     */
    protected $_roleId;
    
    /**
     * Username
     * 
     * @var string 
     */
    protected $_username;
    
    /**
     * Password
     * 
     * @var string 
     */
    protected $_password;
    
    /**
     * User status
     * 
     * @var boolean 
     */
    protected $_valid = false;


    /**
     * Checks if user is exists
     *
     * @return void
     */
    public function getAuth()
    {
        try {
            
            if(empty($this->_username)) {
                throw new Zend_Exception("Variable username is empty!");
            }
            if(empty($this->_password)) {
                throw new Zend_Exception("Variable password is empty!");
            }

            $query = $this->_db
                          ->select()
                          ->from(array("u" => "login_users"),
                                 array("userId", "roleId"))
                          ->join(array("r" => "access_roles"),
                                 "u.roleId = r.roleId",
                                  array("rolename"))
                          ->where("username = ?", $this->_username)
                          ->where("password = ?", $this->_password);
            $result = $this->_db->fetchRow($query);
            
            if($result === false) {
                $this->_valid = false;
            } else {
                $this->_userId = $result["userId"];
                $this->_roleId = $result["roleId"];
                $this->_rolename = $result["rolename"];

                $this->_valid = true;
            }
        } catch (Zend_Exception $e) {

            error_log(print_r($e, true));
            $this->_valid = false;
        }
    }
    
    /**
     * Is user vaild
     * 
     * @return boolean
     */
    public function isVaild()
    {
        return $this->_valid;
    }

    /**
     * Get role id
     * 
     * @return string
     */
    public function getRoleId()
    {
        return $this->_roleId;
    }
        
    /**
     * Get user id
     * 
     * @return string
     */
    public function getUserId()
    {
        return $this->_userId;
    }
    
    /**
     * Get username
     * 
     * @return string
     */
    public function getUsername()
    {
        return $this->_username;
    }
    
    /**
     * Get rolename
     * 
     * @return string
     */
    public function getRolename()
    {
        return $this->_rolename;
    }

    /**
     * Set role id
     * 
     * @param string $roleId
     * 
     * @return Login_Model_Auth
     */
    public function setRoleId($roleId)
    {
        $this->_roleId = $roleId;
        return $this;
    }
    
    /**
     * Set user id
     * 
     * @param string $userId
     * 
     * @return Login_Model_Auth
     */
    public function setUserId($userId)
    {
        $this->_userId = $userId;
        return $this;
    }
    
    /**
     * Set username
     * 
     * @param string $username
     * 
     * @return Login_Model_Auth
     */
    public function setUsername($username)
    {
        $this->_username = $username;
        return $this;
    }
    
    /**
     * Set password
     * 
     * @param string $password
     * 
     * @return Login_Model_Auth
     */
    public function setPassword($password)
    {
        $this->_password = md5($password);
        return $this;
    }
}